package net.hcpme.activity.http;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import net.hcpme.service.ReceiveMessageService;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class LoginAsyncTask extends AsyncTask<String, Integer, String> {

    public interface AsyncResponse {
        void processFinish(String output);
    }

    private AsyncResponse response;
    private ProgressDialog pdialog;
    private Context context;

    private int TIMEOUT = 5 * 1000;

    public LoginAsyncTask(Context context) {
        this.context = context;
        pdialog = new ProgressDialog(this.context, 0);
        pdialog.setTitle("登录中...");
        pdialog.setCancelable(true);
        pdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pdialog.show();
    }

    public void setResponse(AsyncResponse response) {
        this.response = response;
    }

    @Override
    protected String doInBackground(String... arg0) {
        JSONObject user = getLoginJson(arg0[0], arg0[1]);
        // 创建Intent
        Intent intent = new Intent(context, ReceiveMessageService.class);
        intent.putExtra("message", user.toString());
        intent.putExtra("message_type", 1);
        context.startService(intent);
return "";
        /*try {
            // 创建发送方的套接字，IP默认为本地，端口号随机
            DatagramSocket sendSocket = new DatagramSocket();
            byte[] buf = user.toString().getBytes();
            // InetAddress address =
            // InetAddress.getByName(Common.getPropertie(context,
            // "service_ip"));
            // int port = Integer.parseInt(Common.getPropertie(context,
            // "service_port"));
            // Log.e("tag~~~",">>>----------------"+address.getAddress().toString()+"---"+port);
            // 创建发送类型的数据报：
            // DatagramPacket sendPacket = new DatagramPacket(buf, buf.length,
            // InetAddress.getByName("42.121.194.228"), 7000);
            DatagramPacket sendPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("10.240.2.121"), 7000);
            // 通过套接字发送数据：
            sendSocket.send(sendPacket);

            // 确定接受反馈数据的缓冲存储器，即存储数据的字节数组
            byte[] getBuf = new byte[1024];
            // 创建接受类型的数据报
            DatagramPacket getPacket = new DatagramPacket(getBuf, getBuf.length);
            sendSocket.setSoTimeout(TIMEOUT);
            // 通过套接字接受数据
            sendSocket.receive(getPacket);
            // 解析反馈的消息，并打印
            String backMes = new String(getBuf, 0, getPacket.getLength());
            // 关闭套接字
            sendSocket.close();
            return backMes;
        } catch (SocketException e1) {
            e1.printStackTrace();
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "time_out";*/
    }

    // 组装json
    private JSONObject getLoginJson(String userName, String password) {
        // 首先最外层是{}，是创建一个对象
        JSONObject user = new JSONObject();
        try {
            JSONObject accress = new JSONObject();
            accress.put("帐户", userName);
            accress.put("密码", password);
            accress.put("验证码", "");
            JSONObject version = new JSONObject();
            version.put("版本", 1.0);
            version.put("会员", new JSONObject().put("登录", accress));
            user.put("文档", version);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (pdialog != null) {
            pdialog.dismiss();
        }

        response.processFinish(result);
    }

    @Override
    protected void onPreExecute() {
    }

}
